package cn.com.joco.cf.core.cui.dataTable;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.google.common.collect.Lists;

public class DataTableRequest {
	Logger logger = LoggerFactory.getLogger(DataTableRequest.class); 
	private String sEcho="";  //安全字符串
	private int pageNumber=0;
	private int pageSize=0;
	private ArrayList<String> columns=Lists.newArrayList();
	private String sortColumn="";
	private String sortDirection="asc";
	private  DataTableRequest(int pageNumber,int pageSize,ArrayList<String> columns,String sEcho,String sortColumn,String sortDirection) {
		this.pageNumber=pageNumber;
		this.pageSize=pageSize;
		this.columns=columns;
		this.sEcho=sEcho;
		this.sortColumn=sortColumn;
		this.sortDirection=sortDirection;
	}

	
	public static DataTableRequest buildDataTableRequest(HttpServletRequest request){
		Validate.notNull(request,"Request can not br null!");
		int startIndex=Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("iDisplayStart"),"0"));
		int pageSize=Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("iDisplayLength"),"10"));
		int columnsCount=Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("iColumns"),"0"));
		int sortColumnIndex=Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("iSortCol_0"),"-1"));
		String sortDerection=StringUtils.defaultIfEmpty(request.getParameter("sSortDir_0"),"");
		String sEcho=StringUtils.defaultIfEmpty(request.getParameter("sEcho"),"");
		String sortColumn="";
		if(pageSize<=0){
			pageSize=10;
		}
		int pageNumber=startIndex/pageSize;
		ArrayList<String> columns=Lists.newArrayList();
		for(int i=0;i<columnsCount;i++){
			String column=request.getParameter("mDataProp_"+i);
			if(StringUtils.isEmpty(column)){
				break;
			}
			columns.add(column);
		}
		if(StringUtils.equalsIgnoreCase("asc", StringUtils.trim(sortDerection) )){
			sortDerection="asc";
		}
		if(sortColumnIndex!=-1&&columns.size()>sortColumnIndex){
			sortColumn=columns.get(sortColumnIndex);
		}
		return new DataTableRequest(pageNumber,pageSize,columns,sEcho,sortColumn,sortDerection);
	}
	
	public Pageable buildPageable(){
		Sort sort=null;
		if(StringUtils.equalsIgnoreCase("asc", this.sortDirection)){
			sort=new Sort(Direction.ASC,this.sortColumn);
		}else{
			sort=new Sort(Direction.DESC,this.sortColumn);
		}
		return new PageRequest(this.pageNumber, this.pageSize,sort);
	}


	public Logger getLogger() {
		return logger;
	}


	public String getsEcho() {
		return sEcho;
	}


	public int getPageNumber() {
		return pageNumber;
	}


	public int getPageSize() {
		return pageSize;
	}


	public ArrayList<String> getColumns() {
		return columns;
	}


	public String getSortColumn() {
		return sortColumn;
	}


	public String getSortDirection() {
		return sortDirection;
	}


	public void setLogger(Logger logger) {
		this.logger = logger;
	}


	public void setsEcho(String sEcho) {
		this.sEcho = sEcho;
	}


	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public void setColumns(ArrayList<String> columns) {
		this.columns = columns;
	}


	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}


	public void setSortDirection(String sortDirection) {
		this.sortDirection = sortDirection;
	}
	
}
